package org.example.web;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Emp;
import org.example.pojo.Result;
import org.example.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private IEmpService empService;

    @Autowired
    private HttpSession session;

    @PostMapping("/login")
    public Result login(String username, String password){
        Emp emp = empService.login(username, password);
        if(emp == null){
            return Result.fail("用户名或密码错误");
        }
        if(emp.getStatus().equals("1")){
            return Result.fail("该员工已离职");
        }
        //放到session
        session.setAttribute("empSession",emp);
        return Result.success(emp,1);
    }
    @GetMapping("/list")
    public Result list(Integer page,Integer limit,String searchEmpName){
        PageInfo<Emp> empPageInfo = empService.listByPage(page, limit, searchEmpName);
        return new Result(0,"success",empPageInfo.getTotal(),empPageInfo.getList());

    }
    @PostMapping("/add")
    public Result add(Emp emp){
        Emp empSession = (Emp) session.getAttribute("empSession");
        emp.setCreateBy(empSession.getEmpName());
        emp.setCreateTime(new Date());
        int addCount = empService.add(emp);
        if(addCount > 0 ){
            return Result.success(true,1);
        }else {
            return Result.fail("保存失败");
        }
    }

    @PostMapping("/update")
    public Result update(Emp emp){
        log.info(emp.toString());
        Emp empSession = (Emp) session.getAttribute("empSession");
        emp.setUpdateBy(empSession.getEmpName());
        emp.setUpdateTime(new Date());
        int updateResult = empService.update(emp);
        if(updateResult > 0){
            return Result.success(true,1);
        }else {
            return Result.fail("编辑失败");
        }
    }

    @PostMapping("/delete")
    public Result delete(Emp emp){
        Emp empSession = (Emp) session.getAttribute("empSession");

        //逻辑删除
        LambdaUpdateWrapper<Emp> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Emp::getEmpId,emp.getEmpId());
        lambdaUpdateWrapper.set(Emp::getDeleteFlag,1);

        lambdaUpdateWrapper.set(Emp::getUpdateBy,empSession.getEmpName());
        lambdaUpdateWrapper.set(Emp::getUpdateTime,new Date());
        boolean updateResult = empService.update(lambdaUpdateWrapper);
        if(updateResult){
            return Result.success(true,1);
        }else{
            return Result.fail("删除失败");
        }
    }

}
